package com.lijie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijie.entity.Pet;
import com.lijie.service.PetService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Pet)表控制层
 *
 * @author makejava
 * @since 2020-07-10 14:25:53
 */
@Controller
@Api("用户表的操作")
@CrossOrigin
public class PetController {
  @Autowired
  private PetService petService;

    /**
     * 查询所有
     * @param model
     * @return
     */
    @RequestMapping(value = {"/","findAll"})
    public String findAll(Model model){
    List<Pet> list = petService.list();
    model.addAttribute("list",list);
    return "index";
}
@RequestMapping("/add")
    public String add(Pet pet){
    boolean save = petService.save(pet);
    if(save==true){
        return "redirect:/findAll";
    }else {
        return "add";
    }

}

@RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
}


    @RequestMapping("/findByName")
    public String findAllByBreed(Model model,Pet pet){
        if (pet.getPetbreed()>0){
            QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("petBreed",pet.getPetbreed());
            List<Pet> list = petService.list(queryWrapper);
            model.addAttribute("list",list);
        }else{
            List<Pet> list = petService.list();
            model.addAttribute("list",list);
        }
        return "index";

    }

}